package com.doyled.jump.scene;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.Vector;

import com.doyled.jump.framework.GameMouseLocation;
import com.doyled.jump.framework.ResourceManager;
import com.doyled.jump.object.Environment;
import com.doyled.jump.util.Drawing;

public class GameScene extends Canvas implements Runnable {

	private static final long serialVersionUID = 319941709914933335L;

	private boolean running = false;

	private Thread thread;

	private Environment env;

	private GameMouseLocation gameMouseLocation;

	static int WIDTH, HEIGHT;

	BufferedImage background;

	public GameScene() {
		addMouseListener(new MouseProcessing());
	}

	public synchronized void start() {
		if (running) {
			return;
		}

		running = true;
		thread = new Thread(this);
		thread.start();
	}

	public synchronized void stop() {
		if (running) {
			thread.interrupt();
			running = false;
		}
	}

	/*
	 * init game
	 */
	private void init() {
		WIDTH = getWidth();
		HEIGHT = getHeight();

		background = ResourceManager.getInstance().getBackgroundImage();

		gameMouseLocation = new GameMouseLocation();
		env = new Environment(gameMouseLocation);
	}

	/*
	 * Game loop
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		init();
		long lastTime = System.nanoTime();
		double amountOfTicks = 60.0;
		double ns = 1000000000 / amountOfTicks;
		double delta = 0;
		long timer = System.currentTimeMillis();
		int updates = 0;
		int frames = 0;
		while (running) {
			long now = System.nanoTime();
			delta += (now - lastTime) / ns;
			lastTime = now;
			while (delta >= 1) {
				tick();
				updates++;
				delta--;
				render();
				frames++;
			}
			// render();
			// frames++;

			if (System.currentTimeMillis() - timer > 1000) {
				timer += 1000;
				// System.out.println("FPS : " + frames + "Ticks : " + updates);
				frames = 0;
				updates = 0;
			}
		}
	}

	/**
	 * update game
	 */
	private void tick() {
		env.tick();
	}

	/**
	 * render game
	 */
	private void render() {
		BufferStrategy bs = this.getBufferStrategy();
		if (bs == null) {
			this.createBufferStrategy(3);
			return;
		}

		Graphics g = bs.getDrawGraphics();
		// ////////////////////////////////////////

		// Draw here
		g.setColor(Color.white);
		g.fillRect(0, 0, getWidth(), getHeight());
		Drawing.draw(g, background, background.getWidth() / 2,
				background.getHeight() / 2);

		env.render(g);
		// /////////////////////////////////////////
		g.dispose();
		bs.show();

	}

	// get mouse event and add to Game mouse location queue
	class MouseProcessing implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.getButton() == MouseEvent.BUTTON1) {
				gameMouseLocation.addClickPostition(e.getX(), e.getY());
				System.out.println(e.getX() + ":" + e.getY());
			}
		}

		@Override
		public void mouseReleased(MouseEvent e) {
		}

	}

}
